
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <strings.h>
#include <sys/types.h>          /* See NOTES */
#include <sys/socket.h>
#include <arpa/inet.h>
int main()
{
	
	char name[200] = "xiaoming";
	char passwd[200] = "123";
	int sockfd = socket(AF_INET, SOCK_STREAM, 0);
	if(-1 == sockfd)
	{
		perror("socket");
		return -1;
	}
	
	struct sockaddr_in addr;
	addr.sin_family = AF_INET;
	addr.sin_port   = htons(6666);
	addr.sin_addr.s_addr = inet_addr("127.0.0.1");

	if(-1 == bind(sockfd, (struct sockaddr*)&addr,sizeof(addr)))
	{
		perror("bind");
		return -1;
	}

	if(-1 == listen(sockfd, 3))
	{
		perror("listen");
		return -1;
	}

	printf("wait for a client conneted\n");

	int connfd = accept(sockfd, NULL, NULL);
	if(-1 == connfd)
	{
		perror("accept");
		return -1;
	}
	printf("conneted a client\n");
	while(1)
	{
		char buf[20];
		char recv_name[200];	
		char recv_passwd[200];
		read(connfd,buf, sizeof(buf));
		printf("camd is %s\n", buf);
		if(strcmp(buf,"login") == 0)
		{
			read(connfd, recv_name, sizeof(recv_name));
			read(connfd, recv_passwd, sizeof(recv_passwd));
			if((strcmp(name,recv_name) == 0) && (strcmp(passwd,recv_passwd) == 0))
			{
				strcpy(buf, "login is OK");
				write(connfd, buf, sizeof(buf));
			}
			else
			{
				strcpy(buf, "login is default");
				write(connfd, buf, sizeof(buf));
				
			}

		}
		else
		{
			read(connfd, recv_name, sizeof(recv_name));
			read(connfd, recv_passwd, sizeof(recv_passwd));
			
			strcpy(name,recv_name);
			strcpy(passwd,recv_passwd);
		
			strcpy(buf, "register is OK");
			write(connfd, buf, sizeof(buf));
		}

	}

}
